<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/12 15:52
 * Modified Time: 2019/8/12 15:52
 * Version: 2.0
 */

namespace App\Modules\Inventory\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetGoodsCartCriteria extends BaseCriterias
{
    protected $user_id;
    protected $store_id;

    public function __construct($user_id, $store_id)
    {
        $this->user_id = $user_id;
        $this->store_id = $store_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $user_id = $this->user_id;
        $store_id = $this->store_id;
        $model = $model->select([
            'id',
            'goods_id',
            'num'
        ])
            ->with(['goods' => function ($query) {
                $query->select([
                    'goods.id',
                    'goods.name',
                    'small_name',
                    'picture.image',
                    'cat_id'
                ])->with(['goodsCategory' => function ($query) {
                    $query->select(['id', 'name']);
                }])
                    ->leftJoin('picture', 'goods.image', 'picture.id');
            }])->where('user_id', $user_id)
            ->where('store_id', $store_id)
            ->orderBy('id','DESC');

        return $model;
    }
}